package com.ckzp.core;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import cn.hutool.json.JSONUtil;
import com.ckzp.core.json.Jmode;
import com.ckzp.core.json.JsonUtil;
import com.ckzp.core.lc.Aes;
import com.ckzp.core.lc.Aes2;
import com.ckzp.jfinal.base.BaseController;
import com.jfinal.core.Const;
import com.jfinal.log.Log;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by 51594 on 2022/7/27.
 */
public class Results {

    public static Log log = Log.getLog(Results.class);

    public static Results n() {
        return new Results();
    }

    /**
     * 定时器
     */
    public TimeInterval timer = null;

    public static Results n(int code) {
        return new Results(code);
    }

    /**
     * 创建对象同时创建定时器
     *
     * @param code
     * @return
     */
    public static Results nt(int code) {
        Results results = Results.n(code);
        results.timer = DateUtil.timer();
        return results;
    }

    /**
     * 创建对象同时创建定时器
     *
     * @return
     */
    public static Results nt() {
        return nt(0);
    }

    public static Results n(int code, String msg) {
        return new Results(code).setMsg(msg);
    }

    /**
     * 结果 代码
     * 一般
     * 0成功
     * 其它失败
     */
    private int code = 0;

    /**
     * 描术信息简写
     */
    private String msg;
    //int型Key
    private Integer keyint;
    //String key
    private String keystring;
    /**
     * token,不一定用得上
     */
    private Map token = new HashMap();

    /**
     * 描述信息详细
     */
    private String message;
    /**
     * 错误信息
     */
    private String err;

    //对像
    private Object object;
    private Long times;

    //对像
    private Jmode data;

    private String secret;

    private String secret_new;

    public Jmode getData() {
        if (data == null) data = Jmode.n();
        return data;
    }

    public Results setData(Jmode data) {
        this.data = data;
        return this;
    }

    public Object getObject() {
        return object;
    }

    public <T> T getObject(Class<T> types) {
        return (T) object;
    }

    public Results setObject(Object object) {
        this.object = object;
        return this;
    }

    public Results(int code) {
        this.code = code;
    }

    public Results() {
        this.code = 0;
    }

    public Results(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Results(int code, String msg, String message) {
        this.code = code;
        this.msg = msg;
        this.message = message;
    }

    public int getCode() {
        return code;
    }

    public Results setCode(int code) {
        this.code = code;
        return this;
    }

    public String getMsg() {
        return msg;
    }

    public Results setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public String getMessage() {
        return message;
    }

    public Results setMessage(String message) {
        this.message = message;
        return this;
    }

    public Integer getKeyint() {
        return keyint;
    }

    public Results setKeyint(Integer keyint) {
        this.keyint = keyint;
        return this;
    }

    public Long getTimes() {
        return times;
    }

    public Results setTimes(Long times) {
        this.times = times;
        return this;
    }


    public Results showTimes() {
        if (this.timer != null) this.times = timer.interval();
        return this;
    }

    public String getKeystring() {
        return keystring;
    }

    public String getErr() {
        return err;
    }

    public Results setErr(String err) {
        this.err = err;
        return this;
    }

    /**
     * 添加输出数据
     *
     * @param key
     * @param o
     * @return
     */
    public Results addData(String key, Object o) {
        if (this.data == null) this.data = Jmode.n();
        this.data.put(key, o);
        return this;
    }


    public Results setKeystring(String keystring) {
        this.keystring = keystring;
        return this;
    }

    public void setToken(BaseController controller) {
        String tokenId = controller.getAttr(Const.DEFAULT_TOKEN_NAME);
        this.token.put(Const.DEFAULT_TOKEN_NAME, tokenId);
    }

    public String getSecret() {
        return secret;
    }

    public void setSecret(String secret) {
        this.secret = secret;
    }

    public String getSecret_new() {
        return secret_new;
    }

    public void setSecret_new(String secret_new) {
        this.secret_new = secret_new;
    }

    public String toJson() {
        // if (ConfigUtil.DEBUG()) log.info(JsonUtil.toJsonString(this));
        return JsonUtil.toJsonString(this);
    }

    public String formatJson() {
        return JSONUtil.formatJsonStr(toJson());
    }


    public String toSecretJson(String publicKey) {
        //this.setSecret(HutoolRsa.encrypt(JsonUtil.toJsonString(this.getData()), publicKey));
        this.addData("times", DateTime.now());
        this.setSecret(Aes.aesEncrypt(JsonUtil.toJsonString(this.getData())));
        this.setSecret_new(Aes2.aesEncrypt(JsonUtil.toJsonString(this.getData())));
        if (!ConfigUtil.DEBUG()) this.setData(null);
        return JsonUtil.toJsonString(this);
    }


    public String getTokenJson() {
        return JsonUtil.toJsonString(this.token);
    }

    @Override
    public String toString() {
        return "Results{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", keyint=" + keyint +
                ", keystring='" + keystring + '\'' +
                ", message='" + message + '\'' +
                ", object=" + object +
                '}';
    }
}
